package com.leap.controller;

import com.leap.entity.RestBean;
import com.leap.entity.dto.AuthorizeDTO;
import com.leap.entity.vo.ArticleVO;
import com.leap.service.AccountService;
import com.leap.service.ArticleService;
import com.leap.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@Slf4j
@Tag(name = "用户相关接口")
public class AuthController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private JwtUtils utils;

    @GetMapping("/info")
    @Operation(summary = "用户信息获取接口")
    public RestBean<AuthorizeDTO> getInfo(HttpServletRequest request){
        String headerToken = request.getHeader("Authorization");
        log.info("headerToken:{}", headerToken);
        AuthorizeDTO authorizeDTO = accountService.getInfoByToken(headerToken);

        return RestBean.success(authorizeDTO);
    }


    /**
     * 更新用户信息
     * @param authorizeDTO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "用户信息修改接口")
    public RestBean<String> update(@RequestBody AuthorizeDTO authorizeDTO,
                                 HttpServletRequest request) {
        String headerToken = request.getHeader("Authorization");
        accountService.updateByDTO(authorizeDTO,headerToken);

        return RestBean.success("修改成功");
    }

    /**
     * 收藏文章
     * @param articleId
     * @param request
     * @return
     */
    @PostMapping("/like")
    @Operation(summary = "用户收藏文章接口")
    public RestBean<Boolean> clickLike(Long articleId,HttpServletRequest request) {
        String headerToken = request.getHeader("Authorization");
        Long accountId = utils.getAccountIdByToken(headerToken);
        Boolean status = accountService.ChangeLikeByArticleId(articleId, headerToken);

        //刷新缓存列表
        accountService.refreshInteractList(accountId);
        articleService.clearRedisCache();

        return RestBean.success(status);
    }

    /**
     * 点赞评论
     * @param commentId
     * @param request
     * @return
     */
    @PostMapping("/likeComment")
    @Operation(summary = "用户点赞评论接口")
    public RestBean<Boolean> commentLike(Long commentId,HttpServletRequest request) {
        String headerToken = request.getHeader("Authorization");

        Boolean status = accountService.ChangeLikeByCommentId(commentId, headerToken);

        return RestBean.success(status);
    }


}
